namespace Crux.Core.Websockets.Metadata;
/// <summary>
/// InvocationResponseMessage
/// </summary>
public class InvocationResponseMessage : Message
{
    /// <summary>
    ///
    /// </summary>
    public InvocationResponseMessage()
    {
        this.Type = MessageType.InvocationResponse;
    }

    /// <summary>
    ///
    /// </summary>
    [JsonProperty("result")]
    public object? Result
    {
        get
        {
            if (result is null) return result;
            return GetOriginValue(result);
        }
        set
        {
            this.result = value; ;
        }
    }
    /// <summary>
    ///
    /// </summary>
    private static object? GetOriginValue(object value)
    {
        Requires.NotNull(value, nameof(value));

        if (value is not Task task) return value;

        task.ConfigureAwait(false);

        object? result = ((dynamic)task).Result;
        if (null != result && typeof(Task).Equals(result.GetType().BaseType))
        {
            return GetOriginValue(result);
        }

        return result;
    }

    /// <summary>
    ///
    /// </summary>
    private object? result { get; set; }
    /// <summary>
    ///
    /// </summary>
    [JsonProperty("target")]
    public string Target { get; set; } = string.Empty;
    /// <summary>
    ///
    /// </summary>
    [JsonProperty("invocationId")]
    public long InvocationId { get; set; }
}
